package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.domain.Movies;
import com.itheima.domain.MoviesExample;
import com.itheima.mapper.MoviesMapper;
import com.itheima.service.MoviesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MoviesServiceImpl implements MoviesService {
    @Autowired
    private MoviesMapper moviesMapper;

    @Override
    public PageInfo<Movies> findByPage(Integer pageNum, Integer pageSize) {
        //  设置分页条件查询
        PageHelper.startPage(pageNum,pageSize);
        //  设置排序条件
        MoviesExample moviesExample = new MoviesExample();
        moviesExample.setOrderByClause("show_time desc");

        //  执行查询所有
        List<Movies> list = moviesMapper.selectByExample(moviesExample);
        return new PageInfo<>(list);
    }

    @Override
    public PageInfo<Movies> findByPage(Integer pageNum, Integer pageSize, Integer cid, String keyword) {
        //设置分页查询条件
        PageHelper.startPage(pageNum,pageSize);
        //设置排序条件
        MoviesExample moviesExample = new MoviesExample();
        moviesExample.setOrderByClause("show_time desc");
        MoviesExample.Criteria criteria = moviesExample.createCriteria();
        //设置查询条件
        if(cid!=null){
            criteria.andCidEqualTo(cid);
        }
        if(keyword!=null && !"".equals(keyword)){
            criteria.andKeywordLike("%"+keyword+"%");
        }

        //执行查询所有
        List<Movies> list = moviesMapper.selectByExample(moviesExample);
        return new PageInfo<>(list);
    }


    @Override
    public void save(Movies movies) {
        //获取影视id
        Integer id = movies.getId();
        if (id==null){
            //新增影视数据
            moviesMapper.insertSelective(movies);
        }else {
            //修改影视数据
            moviesMapper.updateByPrimaryKeySelective(movies);
        }
    }

    @Override
    public Movies findById(Integer id) {
        return moviesMapper.selectByPrimaryKey(id);
    }
    //根据id删除
    @Override
    public void deleteById(Integer id) {
        moviesMapper.deleteByPrimaryKey(id);
    }
    //批量删除
    @Override
    public void deleteBatch(Integer[] ids) {
        for (Integer id : ids) {
            moviesMapper.deleteByPrimaryKey(id);
        }
    }


}
